package entity.model;

import javafx.collections.ObservableMap;
import misc.Base;
import misc.Utils;

import java.util.LinkedHashSet;
import java.util.Set;

public class EntityModel extends Base{
    public final static EntityModel INSTANCE = new EntityModel();

    private EntityModel(){
    }

    public final ObservableMap<Class<?>, Entity> ENTITY_MAP = Utils.observableMap();

    private final Object LOCK = new Object();

    public void make(Set<Class<?>> classes) {
        trigger(Notification.STARTED);
        final Set<Class<?>> missing = new LinkedHashSet<>(classes);

        new Thread(() -> {
            for (Class<?> aClass : classes)
                new Entity(aClass).addObserver((o, arg) -> {
                    Entity entity = (Entity) arg;

                    missing.remove(entity.getType());

                    /**
                     * TODO FIXME XXX handle enums
                     *
                     * Classes:
                     * - LogoException.Group
                     * - LogoFormat.Size
                     * - ColourwayColour
                     */
                    if (missing.size() <= 2)
                        synchronized (LOCK) {
                            LOCK.notify();
                        }
                });
        }).start();

        synchronized (LOCK){
            try {
                LOCK.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        warning("Missing: " + missing);
    }

    public enum Notification {STARTED}
}
